package com.emex.app.stuff.gui.model;

import com.emex.app.shared.data.JuridicalPerson;
import com.emex.app.stuff.provider.JuridicalPersonDataProvider;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Sergey
 */
public class JuridicalPersontableModel extends AbstractTableModel implements TableModel, TableCrud<JuridicalPerson> {

    /**
     * List of all clients.
     */
    private List<JuridicalPerson> clients;
    /**
     * Column names.
     */
    protected String[] columnNames = new String[]{
        "Название", "Адрес", "Банк", "Телефон", "Сайт"
    };
    /**
     * Column classes
     */
    protected Class[] columnClasses = new Class[]{
        String.class, String.class, String.class, String.class, String.class
    };

    /**
     * {@inheritDoc}
     */
    @Override
    public Class getColumnClass(int index) {
        return columnClasses[index];
    }

    public JuridicalPersontableModel() {
        clients = JuridicalPersonDataProvider.getInstance().getAllJuridicalPersons();
    }

    public JuridicalPersontableModel(List<JuridicalPerson> persons) {
        clients = persons;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getRowCount() {
        return clients.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        //        "Название", "Адрес", "Банк", "Телефон", "Сайт"
        if ((rowIndex >= 0) && (rowIndex < clients.size())
                && (columnIndex >= -1) && (columnIndex < columnNames.length)) {
            switch (columnIndex) {
                case -1: {
                    return clients.get(rowIndex);
                }
                case 0: {
                    return clients.get(rowIndex).getName();
                }
                case 1: {
                    return clients.get(rowIndex).getAddress();
                }
                case 2: {
                    return clients.get(rowIndex).getBank();
                }
                case 3: {
                    return clients.get(rowIndex).getPhone();
                }
                case 4: {
                    return clients.get(rowIndex).getWebSite();
                }
                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public void dataRemoved(JuridicalPerson t) {
        clients.remove(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataAdded(JuridicalPerson t) {
        clients.add(t);
        this.fireTableDataChanged();
    }

    @Override
    public void dataUpdated() {
        this.fireTableDataChanged();
    }

}
